using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
using System.Web;
using System.Web.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace Halide
{
	/// <summary>
	/// The Halide E-mail class provides a set of methods for sending and manipulating e-mail.
	/// </summary>
	/// <example>
	/// Make sure you add SMTP server information to your web.config file before using any e-mail methods.
	/// <code>
	/// &lt;system.net&gt;
	///		&lt;mailSettings&gt;
	///			&lt;smtp&gt;
	///				&lt;network host="mail.server.com"
	///				port="25"
	///				userName="outgoing@server.com"
	///				password="password" /&gt;
	///			&lt;/smtp&gt;
	///		&lt;/mailSettings&gt;
	///	&lt;/system.net&gt;
	/// &lt;appSettings&gt;
	///		&lt;add key="MailSSL" value="true" /&gt;
	/// &lt;/appSettings&gt;
	/// </code>
	/// </example>
	sealed public class Email
	{
		private static string _version = "2008.05.10B";

		/// <summary>
		/// Return the version number of the class; read-only.
		/// </summary>
		/// <value>
		/// A string with the version number of the class.
		/// </value>
		public static string Version
		{
			get
			{
				return _version;
			}
		}

		/// <summary>
		/// No constructor is necessary.
		/// </summary>
		private Email()
	    {
	    }

		/// <summary>
		/// Send an e-mail message with optional attachments.
		/// </summary>
		/// <example>
		/// <code>
		/// ArrayList AttachMe = new ArrayList();
		/// AttachMe.Add("/ftp/file1.pdf");
		/// AttachMe.Add("/ftp/file2.pdf");
		/// 
		/// string result = Halide.Email.SendMail(
		///		"from@domain.com",
		///		"John Sender",
		///		"recipient1@domain.com,recipient2@domain.com",
		///		"Message subject here",
		///		"This is the message body.",
		///		"plaintext",
		///		AttachMe);
		/// </code>
		/// </example>
		/// <param name="senderAddress">From address (i.e. joesmith@party.com).</param>
		/// <param name="senderName">From name (i.e. Joe).</param>
		/// <param name="recipient">To address(es), separated by commas.</param>
		/// <param name="subject">Message subject.</param>
		/// <param name="body">Message body.</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <param name="attachments">ArrayList object with virtual paths to files.</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public static string SendMail(string senderAddress, string senderName, string recipient, string subject, string body, string BodyFormat, ArrayList attachments)
		{
			return SendMail(senderAddress, senderName, recipient, "", "", subject, body, BodyFormat, attachments);
		}

		/// <summary>
		/// Send an e-mail message with optional attachments.
		/// </summary>
		/// <example>
		/// <code>
		/// ArrayList AttachMe = new ArrayList();
		/// AttachMe.Add("/ftp/file1.pdf");
		/// AttachMe.Add("/ftp/file2.pdf");
		/// 
		/// string result = Halide.Email.SendMail(
		///		"from@domain.com",
		///		"John Sender",
		///		"recipient1@domain.com,recipient2@domain.com",
		///		"ccRecip1@domain.com,ccRecip2@domain.com",
		///		"bccRecip1@domain.com,bccRecip2@domain.com",
		///		"Message subject here",
		///		"This is the message body.",
		///		"plaintext",
		///		AttachMe);
		/// </code>
		/// </example>
		/// <param name="senderAddress">From address (i.e. joesmith@party.com).</param>
		/// <param name="senderName">From name (i.e. Joe).</param>
		/// <param name="recipient">To address(es), separated by commas.</param>
		/// <param name="ccList">Comma-separated list of e-mail addresses for carbon copies.</param>
		/// <param name="bccList">Comma-separated list of e-mail addresses for blind carbon copies.</param>
		/// <param name="subject">Message subject.</param>
		/// <param name="body">Message body.</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <param name="attachments">ArrayList object with virtual paths to files.</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public static string SendMail(string senderAddress, string senderName, string recipient, string ccList, string bccList, string subject, string body, string BodyFormat, ArrayList attachments)
		{
			System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

			if (String.IsNullOrEmpty(senderName))
			{
				mail.From = new MailAddress(senderAddress);
			}

			else
			{
				mail.From = new MailAddress(senderAddress, senderName);
			}
			
			mail.Subject = subject;
			mail.Body = body;

			foreach (string address in recipient.Split(',')) mail.To.Add(address);

			if (!String.IsNullOrEmpty(ccList))
				foreach (string address in ccList.Split(',')) mail.CC.Add(address);

			if (!String.IsNullOrEmpty(bccList))
				foreach (string address in bccList.Split(',')) mail.Bcc.Add(address);

			string result = "SUCCESS";

			try
			{
				mail.IsBodyHtml = (BodyFormat.ToLower() == "html" ? true : false);

				if (attachments != null)
				{
					if (attachments.Count > 0)
					{
						foreach (string attachment in attachments)
						{
							System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(attachment), MediaTypeNames.Application.Octet);
							mail.Attachments.Add(data);
						}
					}
				}

				SmtpClient smtp = new SmtpClient();

				if (ConfigurationManager.AppSettings["MailSSL"] != null)
				{
					smtp.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["MailSSL"]);
				}

				smtp.Send( mail );
				
				mail.Dispose();
			}

			catch (Exception err)
			{
				result = err.ToString();
			}

			return result;
		}

		/// <summary>
		/// Send an e-mail message.
		/// </summary>
		/// <example>
		/// <code>
		/// string result = Halide.Email.SendMail(
		///		"from@domain.com",
		///		"recipient1@domain.com,recipient2@domain.com",
		///		"Message subject here",
		///		"This is the message body.",
		///		"plaintext");
		/// </code>
		/// </example>
		/// <param name="sender">From address (i.e. joesmith@party.com).</param>
		/// <param name="recipient">To address(es), separated by commas.</param>
		/// <param name="subject">Message subject.</param>
		/// <param name="body">Message body.</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public static string SendMail(string sender, string recipient, string subject, string body, string BodyFormat)
	    {
			ArrayList attachments = new ArrayList();
			return SendMail(sender, "", recipient, subject, body, BodyFormat, attachments);
		}

		/// <summary>
		/// Send an e-mail message.
		/// </summary>
		/// <example>
		/// <code>
		/// string result = Halide.Email.SendMail(
		///		"from@domain.com",
		///		"John Sender",
		///		"recipient1@domain.com,recipient2@domain.com",
		///		"Message subject here",
		///		"This is the message body.",
		///		"plaintext");
		/// </code>
		/// </example>
		/// <param name="sender">From address (i.e. joesmith@party.com).</param>
		/// <param name="senderName">From name (i.e. Joe Smith).</param>
		/// <param name="recipient">To address(es), separated by commas.</param>
		/// <param name="subject">Message subject.</param>
		/// <param name="body">Message body.</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public static string SendMail(string sender, string senderName, string recipient, string subject, string body, string BodyFormat)
	    {
			ArrayList attachments = new ArrayList();
			return SendMail(sender, senderName, recipient, subject, body, BodyFormat, attachments);
		}

		/// <summary>
		/// Send an e-mail message.
		/// </summary>
		/// <example>
		/// <code>
		/// string result = Halide.Email.SendMail(
		///		"from@domain.com",
		///		"John Sender",
		///		"recipient1@domain.com,recipient2@domain.com",
		///		"ccRecip1@domain.com,ccRecip2@domain.com",
		///		"bccRecip1@domain.com,bccRecip2@domain.com",
		///		"Message subject here",
		///		"This is the message body.",
		///		"plaintext");
		/// </code>
		/// </example>
		/// <param name="sender">From address (i.e. joesmith@party.com).</param>
		/// <param name="senderName">From name (i.e. Joe Smith).</param>
		/// <param name="recipient">To address(es), separated by commas.</param>
		/// <param name="ccList">Comma-separated list of e-mail addresses for carbon copies.</param>
		/// <param name="bccList">Comma-separated list of e-mail addresses for blind carbon copies.</param>
		/// <param name="subject">Message subject.</param>
		/// <param name="body">Message body.</param>
		/// <param name="BodyFormat">"html" or "plaintext".</param>
		/// <returns>"SUCCESS", or an error message.</returns>
		public static string SendMail(string sender, string senderName, string recipient, string ccList, string bccList, string subject, string body, string BodyFormat)
		{
			ArrayList attachments = new ArrayList();
			return SendMail(sender, senderName, recipient, ccList, bccList, subject, body, BodyFormat, attachments);
		}

	// Outer Identify class
	}

// Outer namespace
}